package com.jy.datapipeline.importserver.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jy.client.common.DBConfig;
import com.jy.client.jdbc.DataBusHelper;
import com.jy.client.jdbc.conf.Configuration;
import com.jy.datapipeline.common.util.AssertUtil;
import com.jy.datapipeline.common.constants.ExpConstants;
import com.jy.datapipeline.importserver.common.constants.KeyConstant;
import com.jy.datapipeline.importserver.common.enums.impl.ReturnEnum;
import com.jy.datapipeline.common.exception.GlobalException;
import com.jy.datapipeline.common.util.ConvertUtil;
import com.jy.datapipeline.importserver.entity.ImportSqlldrInfo;
import com.jy.datapipeline.importserver.entity.manager.EntityManager;
import com.jy.datapipeline.importserver.mapper.ImportSqlldrInfoMapper;
import com.jy.datapipeline.importserver.service.ImportSqlldrInfoService;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @类名 ImportDataSourceServiceImpl
 * @描述 sqlldrInfo模块操作业务
 * @作者 白条君
 * @创建日期 2023/4/12 20:02
 * @版本 1.0
 */
@Service
public class ImportSqlldrInfoServiceImpl implements ImportSqlldrInfoService {

    @Resource
    private ImportSqlldrInfoMapper importSqlldrInfoMapper;

    @Resource
    private RestTemplate restTemplate;

    @Override
    public ImportSqlldrInfo getImportSqlldrInfo(String jdbcUrl) {
        //获取tns串
        DataBusHelper.init(jdbcUrl);
        DBConfig.DBInfo dbInfo = DBConfig.getDBInfo(jdbcUrl);
        String tnsUrl = dbInfo.dbUrl;

        //解析jdbcUrl
        Configuration configuration = new Configuration();
        configuration.setUrl(jdbcUrl);
        String url = "http://" + configuration.getHostName() + ":" + configuration.getPort() + "/oci/sqlldrConfig";

        //参数构建
        Map<String, Object> params = new HashMap<>();
        params.put("appName", "DATA_PIPELINE");
        params.put("url", tnsUrl);

        Object object = restTemplate.postForObject(url, params, Object.class);

        //解析结果获取sqlldrurl
        Map<String, Object> objectMap = ConvertUtil.convertMap(object);
        Map<String, Object> dataMap = ConvertUtil.convertMap(objectMap.get("data"));
        String sqlldrUrl = "";
        if (ObjectUtil.isNotNull(dataMap.get("sqlldrBaseUrl"))) {
            sqlldrUrl = (String) dataMap.get("sqlldrBaseUrl");
            sqlldrUrl = sqlldrUrl.substring(sqlldrUrl.indexOf("//") + 2, sqlldrUrl.lastIndexOf("/"));
        }

        //添加到importSqlldrInfo
        ImportSqlldrInfo importSqlldrInfo = (ImportSqlldrInfo) EntityManager.create(KeyConstant.IMPORT_SQLLDR_INFO_KEY);
        importSqlldrInfo.setSqlldrUrl(sqlldrUrl);
        importSqlldrInfo.setTnsUrl(tnsUrl.substring(tnsUrl.indexOf("@") + 1));
        return importSqlldrInfo;
    }

    @Override
    public void addSqlldrInfo(ImportSqlldrInfo importSqlldrInfo) throws GlobalException {
        String username = importSqlldrInfo.getUsername();
        String password = importSqlldrInfo.getPassword();
        String sqlldrParams = importSqlldrInfo.getSqlldrParams();
        String sid = importSqlldrInfo.getSid();
        if (StringUtils.isNullOrEmpty(username) || StringUtils.isNullOrEmpty(password)
                || StringUtils.isNullOrEmpty(sqlldrParams) || StringUtils.isNullOrEmpty(sid)) {
            throw new GlobalException(ReturnEnum.PARAM_ERROR.getCode(), ExpConstants.PARAMS_EXP);
        }

        int res = importSqlldrInfoMapper.insertSelective(importSqlldrInfo);
        if (res == 0) {
            throw new GlobalException(ExpConstants.ADD_FAIL);
        }
    }

    @Transactional
    @Override
    public void deleteSqlldrInfo(String sqlldrInfoId) throws GlobalException {
        Long id = importSqlldrInfoMapper.selectSqlldrInfoId(sqlldrInfoId);

        Integer resource = importSqlldrInfoMapper.selectResource(id);
        if (resource == null) {
            throw new GlobalException(ReturnEnum.SERVER_ERROR.getCode(), ExpConstants.RESOURCE_EXP);
        }

        int res = importSqlldrInfoMapper.deleteByPrimaryKey(id);
        if (res == 0) {
            throw new GlobalException(ExpConstants.DELETE_FAIL);
        }
    }

    @Override
    public void batchRemove(Set<String> sqlldrInfoIds) {
        int size = importSqlldrInfoMapper.batchDelete(sqlldrInfoIds);
        AssertUtil.totalAffectedRows(size, sqlldrInfoIds, ExpConstants.BATCH_DELETE_FAIL);
    }
}
